package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.project.entity.Menu;

/**
 * 菜单表 数据层
 * 
 * @author ruoyi
 */
@Mapper
public interface MenuMapper {

	/**
	 * 查询系统所有菜单（含按钮）
	 * 
	 * @return 菜单列表
	 */
	@Select("select menu_id, menu_name, parent_id, order_num, url, target, menu_type, visible, is_refresh, ifnull(perms,'') as perms, icon, create_by, create_time from t_menu order by parent_id, order_num")
	public List<Menu> selectMenuAll();

	/**
	 * 根据用户ID查询菜单
	 * 
	 * @param userId 用户ID
	 * @return 菜单列表
	 */
	@Select("select distinct m.menu_id, m.parent_id, m.menu_name, m.url, m.visible, m.is_refresh, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time "
			+ "from t_menu m " + "left join t_role_menu rm on m.menu_id = rm.menu_id "
			+ "left join t_user_role ur on rm.role_id = ur.role_id " + "LEFT JOIN t_role ro on ur.role_id = ro.role_id "
			+ "where ur.user_id = #{userId} " + "order by m.parent_id, m.order_num")
	public List<Menu> selectMenuAllByUserId(Long userId);

	/**
	 * 查询系统正常显示菜单（不含按钮）
	 * 
	 * @return 菜单列表
	 */
	@Select("select distinct m.menu_id, m.parent_id, m.menu_name, m.url, m.visible, m.is_refresh, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time "
			+ "from t_menu m " + "where m.menu_type in ('M', 'C') and m.visible = 0 "
			+ "order by m.parent_id, m.order_num")
	public List<Menu> selectMenuNormalAll();

	/**
	 * 根据用户ID查询菜单
	 * 
	 * @param userId 用户ID
	 * @return 菜单列表
	 */
	@Select("select distinct m.menu_id, m.parent_id, m.menu_name, m.url, m.visible, m.is_refresh, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time "
			+ "from t_menu m " + "left join t_role_menu rm on m.menu_id = rm.menu_id "
			+ "left join t_user_role ur on rm.role_id = ur.role_id " + "LEFT JOIN t_role ro on ur.role_id = ro.role_id "
			+ "where ur.user_id = #{userId} and m.menu_type in ('M', 'C') and m.visible = 0 AND ro.status = 0 "
			+ "order by m.parent_id, m.order_num")
	public List<Menu> selectMenusByUserId(Long userId);

	/**
	 * 根据用户ID查询权限
	 * 
	 * @param userId 用户ID
	 * @return 权限列表
	 */
	@Select("select distinct m.perms " + "from t_menu m " + "left join t_role_menu rm on m.menu_id = rm.menu_id "
			+ "left join t_user_role ur on rm.role_id = ur.role_id " + "left join t_role r on r.role_id = ur.role_id "
			+ "where m.visible = '0' and r.status = '0' and ur.user_id = #{userId}")
	public List<String> selectPermsByUserId(Long userId);

	/**
	 * 根据角色ID查询权限
	 * 
	 * @param roleId 角色ID
	 * @return 权限列表
	 */
	@Select("select distinct m.perms " + "from t_menu m " + "left join t_role_menu rm on m.menu_id = rm.menu_id "
			+ "where m.visible = '0' and rm.role_id = #{roleId}")
	public List<String> selectPermsByRoleId(Long roleId);

	/**
	 * 根据角色ID查询菜单
	 * 
	 * @param roleId 角色ID
	 * @return 菜单列表
	 */
	@Select("select concat(m.menu_id, ifnull(m.perms,'')) as perms " + "from t_menu m "
			+ "left join t_role_menu rm on m.menu_id = rm.menu_id " + "where rm.role_id = #{roleId} "
			+ "order by m.parent_id, m.order_num")
	public List<String> selectMenuTree(Long roleId);

	/**
	 * 查询系统菜单列表
	 * 
	 * @param menu 菜单信息
	 * @return 菜单列表
	 */
	@Select("<script>select menu_id, menu_name, parent_id, order_num, url, target, menu_type, visible, is_refresh, ifnull(perms,'') as perms, icon, create_by, create_time "
			+ "from t_menu " + "<where> "
			+ "<if test=\"menuName != null and menuName != ''\">AND menu_name like concat('%', #{menuName}, '%')</if> "
			+ "<if test=\"visible != null and visible != ''\">AND visible = #{visible}</if> " + "</where> "
			+ "order by parent_id, order_num</script>")
	public List<Menu> selectMenuList(Menu menu);

	/**
	 * 查询系统菜单列表
	 * 
	 * @param menu 菜单信息
	 * @return 菜单列表
	 */
	@Select("<script>select distinct m.menu_id, m.parent_id, m.menu_name, m.url, m.visible, m.is_refresh, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time "
			+ "from t_menu m " + "left join t_role_menu rm on m.menu_id = rm.menu_id "
			+ "left join t_user_role ur on rm.role_id = ur.role_id " + "LEFT JOIN t_role ro on ur.role_id = ro.role_id "
			+ "where ur.user_id = #{params.userId} "
			+ "<if test=\"menuName != null and menuName != ''\">AND m.menu_name like concat('%', #{menuName}, '%')</if> "
			+ "<if test=\"visible != null and visible != ''\">AND m.visible = #{visible}</if> "
			+ "order by m.parent_id, m.order_num</script>")
	public List<Menu> selectMenuListByUserId(Menu menu);

	/**
	 * 删除菜单管理信息
	 * 
	 * @param menuId 菜单ID
	 * @return 结果
	 */
	@Delete("delete from t_menu where menu_id = #{menuId} or parent_id = #{menuId}")
	public int deleteMenuById(Long menuId);

	/**
	 * 根据菜单ID查询信息
	 * 
	 * @param menuId 菜单ID
	 * @return 菜单信息
	 */
	@Select("SELECT t.menu_id, t.parent_id, t.menu_name, t.order_num, t.url, t.target, t.menu_type, t.visible, t.is_refresh, t.perms, t.icon, t.remark, "
			+ "(SELECT menu_name FROM t_menu WHERE menu_id = t.parent_id) parent_name " + "FROM t_menu t "
			+ "where t.menu_id = #{menuId}")
	public Menu selectMenuById(Long menuId);

	/**
	 * 查询菜单数量
	 * 
	 * @param parentId 菜单父ID
	 * @return 结果
	 */
	@Select("select count(1) from t_menu where parent_id=#{menuId}")
	public int selectCountMenuByParentId(Long parentId);

	/**
	 * 新增菜单信息
	 * 
	 * @param menu 菜单信息
	 * @return 结果
	 */
	@Insert("<script>insert into t_menu(" + "<if test=\"menuId != null and menuId != 0\">menu_id,</if>"
			+ "<if test=\"parentId != null and parentId != 0\">parent_id,</if>"
			+ "<if test=\"menuName != null and menuName != ''\">menu_name,</if>"
			+ "<if test=\"orderNum != null\">order_num,</if>" + "<if test=\"url != null and url != ''\">url,</if>"
			+ "<if test=\"target != null and target != ''\">target,</if>"
			+ "<if test=\"menuType != null and menuType != ''\">menu_type,</if>"
			+ "<if test=\"visible != null\">visible,</if>" + "<if test=\"isRefresh != null\">is_refresh,</if>"
			+ "<if test=\"perms !=null and perms != ''\">perms,</if>"
			+ "<if test=\"icon != null and icon != ''\">icon,</if>"
			+ "<if test=\"remark != null and remark != ''\">remark,</if>"
			+ "<if test=\"createBy != null and createBy != ''\">create_by,</if>" + "create_time" + ")values("
			+ "<if test=\"menuId != null and menuId != 0\">#{menuId},</if>"
			+ "<if test=\"parentId != null and parentId != 0\">#{parentId},</if>"
			+ "<if test=\"menuName != null and menuName != ''\">#{menuName},</if>"
			+ "<if test=\"orderNum != null\">#{orderNum},</if>" + "<if test=\"url != null and url != ''\">#{url},</if>"
			+ "<if test=\"target != null and target != ''\">#{target},</if>"
			+ "<if test=\"menuType != null and menuType != ''\">#{menuType},</if>"
			+ "<if test=\"visible != null\">#{visible},</if>" + "<if test=\"isRefresh != null\">#{isRefresh},</if>"
			+ "<if test=\"perms !=null and perms != ''\">#{perms},</if>"
			+ "<if test=\"icon != null and icon != ''\">#{icon},</if>"
			+ "<if test=\"remark != null and remark != ''\">#{remark},</if>"
			+ "<if test=\"createBy != null and createBy != ''\">#{createBy},</if>" + "sysdate()" + ")</script>")
	public int insertMenu(Menu menu);

	/**
	 * 修改菜单信息
	 * 
	 * @param menu 菜单信息
	 * @return 结果
	 */
	@Update("<script>update t_menu" + "<set>"
			+ "<if test=\"menuName != null and menuName != ''\">menu_name = #{menuName},</if>"
			+ "<if test=\"parentId != null and parentId != 0\">parent_id = #{parentId},</if>"
			+ "<if test=\"orderNum != null\">order_num = #{orderNum},</if>"
			+ "<if test=\"url != null\">url = #{url},</if>"
			+ "<if test=\"target != null and target != ''\">target = #{target},</if>"
			+ "<if test=\"menuType != null and menuType != ''\">menu_type = #{menuType},</if>"
			+ "<if test=\"visible != null\">visible = #{visible},</if>"
			+ "<if test=\"isRefresh != null\">is_refresh = #{isRefresh},</if>"
			+ "<if test=\"perms !=null\">perms = #{perms},</if>"
			+ "<if test=\"icon !=null and icon != ''\">icon = #{icon},</if>"
			+ "<if test=\"remark != null\">remark = #{remark},</if>"
			+ "<if test=\"updateBy != null and updateBy != ''\">update_by = #{updateBy},</if>"
			+ "update_time = sysdate()" + "</set>" + "where menu_id = #{menuId}</script>")
	public int updateMenu(Menu menu);

	/**
	 * 保存菜单排序
	 * 
	 * @param menuIds   菜单ID
	 * @param orderNums 排序ID
	 */
	@Update("update t_menu set order_num = #{orderNum} where menu_id = #{menuId}")
	public void updateMenuSort(Menu menu);

	/**
	 * 校验菜单名称是否唯一
	 * 
	 * @param menuName 菜单名称
	 * @param parentId 父菜单ID
	 * @return 结果
	 */
	@Select("select menu_id, menu_name, parent_id, order_num, url, target, menu_type, visible, is_refresh, ifnull(perms,'') as perms, icon, create_by, create_time "
			+ "from t_menu where menu_name=#{menuName} and parent_id = #{parentId} limit 1")
	public Menu checkMenuNameUnique(@Param("menuName") String menuName, @Param("parentId") Long parentId);
}
